<?php

class Globals {
	public $debug;
	public $server;
	public $db;
	public $sqlerror;
	public $pektis;
	public $partida;
	public $theatis;
	public $thesi;
	
	public function __construct() {
		$this->debug = (isset($_REQUEST) && is_array($_REQUEST) &&
			array_key_exists("debug", $_REQUEST));

		if (!isset($_SERVER)) {
			die("_SERVER: not set");
		}
		
		if (!is_array($_SERVER)) {
			die("_SERVER not an array");
		}
		
		if (!array_key_exists("SERVER_NAME", $_SERVER)) {
			die("SERVER_NAME: index not found");
		}

		$this->sqlerror = FALSE;
		switch ($_SERVER["SERVER_NAME"]) {
		case "localhost":
		case "127.0.0.1":
			$this->server = "http://" . $_SERVER["SERVER_NAME"] . "/panospanos1/";
			$this->db = mysqli_connect($_SERVER["SERVER_NAME"], "root", "", "fantasmatakia");
			$this->sqlerror = TRUE;
			break;
		case "fantasmatakia.webatu.com":
		case "www.fantasmatakia.webatu.com":
			$this->server = "http://" . $_SERVER["SERVER_NAME"] . "/";
			$this->db = mysqli_connect("mysql12.000webhost.com",
				"a3945695_fantasm", "tri03jan", "a3945695_fantasm");
			break;
		default:
			die($_SERVER["SERVER_NAME"] . ": cannot open database");
			break;
		}
		
		@mysqli_set_charset($this->db, 'UTF8');
		if ($this->db === FALSE) {
			die("database connect fail");
		}

		$this->pektis = NULL;
		$this->partida = NULL;
		$this->theatis = NULL;
		$this->thesi = NULL;
	}

	public function set_pektis() {
		if (!isset($_SESSION)) {
			die("_SESSION: not set");
		}

		if (!is_array($_SESSION)) {
			die("_SESSION: not an array");
		}

		if (!array_key_exists("login", $_SESSION)) {
			$this->pektis = new Pektis();
			return;
		}

		$this->pektis = new Pektis($_SESSION['login']);
		if (!isset($this->pektis->login)) {
			unset($_SESSION['login']);
		}
	}

	public function check_pektis() {
		$this->set_pektis();
		if ($this->is_pektis()) {
			return;
		}

		die("Απροσδιόριστος παίκτης");
	}

	public function set_partida() {
//ελεγχος αν είμαι παίκτης.
//έλεγχος στον πίνακα θεατών.
//έλεγχος στον πίνακα παρτίδων.
	}

	public function is_pektis() {
		if (!isset($this->pektis)) {
			return FALSE;
		}

		if (!isset($this->pektis->login)) {
			return FALSE;
		}

		return TRUE;
	}

	public function not_pektis() {
		return (!$this->is_pektis());
	}

	public function is_theatis() {
		if (!isset($this->theatis)) {
			return FALSE;
		}

		return $this->theatis;
	}

	public static function perastike($parametros, $verbose = TRUE) {
		if (!isset($_REQUEST)) {
			die("_REQUEST: not set");
		}
		
		if (!is_array($_REQUEST)) {
			die("_REQUEST not an array");
		}
		
		if (array_key_exists($parametros, $_REQUEST)) {
			return TRUE;
		}

		if ($verbose) {
			print $parametros . ": index not found";
		}

		return FALSE;
	}
	
	public static function parametros($parametros) {
		if (self::perastike($parametros)) {
			return $_REQUEST[$parametros];
		}

		die(0);
	}
	
	public function asfales($s) {
		return mysqli_real_escape_string($this->db, $s);
	}

	public function query($query) {
		$result = ($this->sqlerror ? mysqli_query($this->db, $query)
			: @mysqli_query($this->db, $query));
		if ($result === FALSE) {
			print "sql error";
			if ($this->sqlerror) {
				print " (" . $query . ")";
			}
			die(2);
		}
		return $result;
	}

	public static function json_safe($s) {
		return str_replace("'", "\\'", $s);
	}

	public static function get_line($fp) {
		$line = fgets($fp);
		if (!$line) { return FALSE; }

		$line = preg_replace("/[\r\n]/", "", $line);
		return $line;
	}

	public static function put_line($fp, $s) {
		return fwrite($fp, $s . "\n");
	}
}

mb_internal_encoding('UTF-8');
mb_regex_encoding('UTF-8');

global $globals;
$globals = new Globals();

?>
